package itesm.mx;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidProject extends Activity {
	
	public static final String DROID_TRIVIA_PREFERENCES = "android_trivia_prefs";

	public static final int QUIZ_FLAGS = 0;
	public static final int QUIZ_MAPS = 1;
	public static final int QUIZ_CAPITALS = 2;

	private static final int DIALOG_TAKE_CHOICE = 0;
	private static final int DIALOG_HELP = 1;
	private static final int DIALOG_ABOUT = 2;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((Button) findViewById(R.id.btnTakeQuiz)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_TAKE_CHOICE);
			}
		});
		
		((Button) findViewById(R.id.btnHighScore)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intencion;
				intencion = new Intent(getBaseContext(), HighScores.class);
				startActivity(intencion);
			}
		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_TAKE_CHOICE:
			AlertDialog.Builder quizChoiceBuilder = new AlertDialog.Builder(this);
			quizChoiceBuilder.setTitle(R.string.lblTitleSelectQuiz);
			quizChoiceBuilder.setSingleChoiceItems(R.array.take_quiz_choice, -1, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					Intent intencion;
					switch (which) {
					case QUIZ_FLAGS:
						intencion = new Intent(AndroidProject.this, FlagQuizActivity.class);
						startActivity(intencion);
						break;
					case QUIZ_CAPITALS:
						intencion = new Intent(AndroidProject.this, CapitalsQuizActivity.class);
						startActivity(intencion);
						break;
					case QUIZ_MAPS:
						intencion = new Intent(AndroidProject.this, MapsQuizActivity.class);
						startActivity(intencion);
						break;
					}
				}
			});
			quizChoiceBuilder.setNegativeButton(R.string.lblCancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			dialog = quizChoiceBuilder.create();
			break;
			
		case DIALOG_ABOUT:
			AlertDialog.Builder aboutDialogBuilder = new AlertDialog.Builder(this);
			aboutDialogBuilder.setTitle(R.string.about_title);
			aboutDialogBuilder.setMessage(R.string.lblAcercaDe);
			aboutDialogBuilder.setPositiveButton(R.string.lblClose, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialog = aboutDialogBuilder.create();
			break;
		
		case DIALOG_HELP:
			AlertDialog.Builder helpDialogBuilder = new AlertDialog.Builder(this);
			helpDialogBuilder.setTitle(R.string.help_title);
			helpDialogBuilder.setMessage(R.string.lblAyuda);
			helpDialogBuilder.setPositiveButton(R.string.lblClose, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialog = helpDialogBuilder.create();
			break;
		}
		return dialog;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater md = getMenuInflater();
		md.inflate(R.menu.menu_principal, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.menuAcercaDe:
			showDialog(DIALOG_ABOUT);
			break;
		case R.id.menuSalir:
			finish();
			break;
		case R.id.menuAyuda:
			showDialog(DIALOG_HELP);
			break;
		}
		
		return true;
	}
}